<template>
  <div>
    <el-dialog title="来料检验单详情"  width="1200px" @open="open" class="icon-dialog" :visible.sync="show" :before-close="closeForm" append-to-body >
      <div>
        <el-button type="primary" size="small" icon="el-icon-check" @click="auditInspect" :disabled="checked" >审核来料检验单</el-button>
      </div>
      <el-form ref="form" :model=" inspectform " size="small" label-width="80px" :inline="true" class="header">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="单据编号:">{{ inspectform.billNo }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收料通知单:" class="no-wrap-label">{{ inspectform.receiveBillNo }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商名称:" class="no-wrap-label">{{ inspectform.supplierName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购员:" class="no-wrap-label">{{ inspectform.purChaser }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建日期:" class="no-wrap-label">{{ inspectform.createTime }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建人:" class="no-wrap-label">{{ inspectform.createUserName }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="质检员:" class="no-wrap-label">{{ inspectform.inspectUser }}</el-form-item>
          </el-col>
        </el-row>

        <br />
      </el-form>

      <el-table :data="inspectList" height="calc(100vh - 300px)">
        <!--        <el-table-column label="No." width="60" align="right" type="index"></el-table-column>-->
        <el-table-column label="单据编号" width="120" prop="billNo"></el-table-column>-->
        <el-table-column label="是否检查" width="120" prop="isChecked">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.isChecked"
                       :disabled="checked"
                       active-text="是"
                       inactive-text="否"
                       @change="switch1Change(scope.row)"/>
          </template>
        </el-table-column>-->
        <el-table-column label="收料单号" width="120" prop="receiveBillNo"></el-table-column>-->
        <el-table-column label="物料编码" width="120" prop="materialNo"></el-table-column>
        <el-table-column label="物料名称" width="120" prop="materialName"></el-table-column>
        <el-table-column label="规格型号" width="120" prop="specification"></el-table-column>
        <el-table-column label="单位名称" width="120" prop="unitName"></el-table-column>
        <el-table-column label="单位编码" width="120" prop="unitNo"></el-table-column>
        <el-table-column label="批号" width="80" prop="flot"></el-table-column>
        <el-table-column label="检验数量" width="80" prop="inspectQty"></el-table-column>
        <el-table-column label="不合格数量" width="80" prop="unqualifiedQty">
          <template slot-scope="scope">
            <el-input  v-model="scope.row.unqualifiedQty" @input="handleQuality(scope.row)"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="合格数量" width="80" prop="qualifiedQty"></el-table-column>
        <el-table-column label="检验结果" width="80" prop="result"></el-table-column>

      </el-table>


    </el-dialog>

  </div>
</template>
<script>
import { detail,changeInspectIsChecked,auditInspect  } from '@/api/app/inspect'
import {saveInspect} from "@/api/app/receive";
export default {
	name:'inspectdetail',
	props: {
		show: {
			type: Boolean,
			default: true
		},
		size: {
			type: String,
			default: 'mini'
		},
		info: {
			type: Object,
		},
	},
	data() {
		return {
      inspectform:{},
      inspectList:[],
      checked:false
		}
	},
	watch:{
		show(val){
			if(val){
			}
		}
	},
	methods: {
    //审核来料检验单
    auditInspect(){
      //添加校验，必须全部检查后允许审核
      let check = this.inspectList.every(function(i){
        return i.isChecked == true;
      })
      if (!check){
        this.$message({
          message:'请全部检验后审核',
          type:'warning'
        })
        return
      }
      this.$confirm('是否确认审核', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '正在审核'
        });
        auditInspect({billNo : this.inspectform.billNo}).then(res =>{
          if (res.data){
            this.$message({
              type: 'success',
              message: '审核成功!'
            });
            this.$emit('refresh_list'); // ✅ 通知父组件刷新
            this.closeForm()

          }
        })
      })
    },
    //修改是否检查的状态
    switch1Change(row){
      let _this =this
      changeInspectIsChecked({val:row,isChecked:row.isChecked }).then(res =>{
        if (res.data){
          this.$message({
            message:'保存成功',
            type:'success'
          })
          _this.detail()
        }
      })
    },
    //填写合格数量
    handleQuality(e){
      if (e.unqualifiedQty <= e.inspectQty ){
        const v = e.unqualifiedQty
        const zero = /^(0{2,})|[^0-9]/g
        let final = 0
        if (!v) {
          final = 0
        } else {
          final = v.toString().replace(zero, (v) => {
            return 0
          })

          if (final.split('')[0] * 1 === 0) {
            final = final.slice(1) - 0 || 0
          }
        }
        this.$nextTick(() => {
          e.unqualifiedQty = final.toString() || '0'
        })
        //不合格数量不允许大于检验数量
        e.qualifiedQty =e.inspectQty - e.unqualifiedQty
      } else{
        this.$message({
          message:'不合格数量不允许大于检验数量',
          type:'warning'
        })
        e.unqualifiedQty = 0
        e.qualifiedQty = e.inspectQty
      }

    },
		open(){
			detail(this.info).then(res => {
        if (res.status == 200) {
          this.inspectform = res.data.data;
          this.inspectList = res.data.dataList;
          this.checkStatus()

        } else {
          this.$message.error('获取详情失败2，请稍后再试');
        }
      }).catch(error => {

        this.$message.error('获取详情失败，请稍后再试');
      });
		},
    checkStatus(){
      console.log(this.inspectform.status )
      if (this.inspectform.status === 'save'){
        this.checked = false;
      } else {
        this.checked = true;
      }
    },
		closeForm(){
			this.$emit('update:show', false)
		}
	}
}
</script>
<style  lang="scss">
@import '@/assets/scss/common.scss';
.no-wrap-label .el-form-item__label {
  white-space: nowrap; /* 强制不换行 */
}
</style>
